package com.vaadin.thomas.contenttool.ui;

import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.thomas.contenttool.ui.events.LangChangedEvent;
import com.vaadin.thomas.contenttool.ui.events.LangChangedListener;
import com.vaadin.thomas.contenttool.ui.events.MenuFilterEvent;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;

public class MenuFilter extends HorizontalLayout implements LangChangedListener {

	private static final long serialVersionUID = 5006504092988390112L;
	private Label caption;
	private TextField filter;
	private Button clear;

	public MenuFilter() {

		setWidth(100, UNITS_PERCENTAGE);

		caption = new Label("");
		caption.setSizeUndefined();
		addComponent(caption);
		setComponentAlignment(caption, Alignment.MIDDLE_LEFT);

		filter = new TextField();
		filter.setWidth(100, UNITS_PERCENTAGE);
		filter.setImmediate(true);
		filter.addListener(new ValueChangeListener() {

			private static final long serialVersionUID = -7202245147021159072L;

			@Override
			public void valueChange(ValueChangeEvent event) {
				MainLayout.bb().fire(
						new MenuFilterEvent((String) event.getProperty()
								.getValue()));
			}
		});
		addComponent(filter);
		setExpandRatio(filter, 1);

		clear = new Button();
		clear.addListener(new ClickListener() {

			private static final long serialVersionUID = -6436117074218098820L;

			@Override
			public void buttonClick(ClickEvent event) {
				filter.setValue("");
				MainLayout.bb().fire(new MenuFilterEvent(null));
			}
		});
		addComponent(clear);

		langChanged(null);
	}

	@Override
	public void langChanged(LangChangedEvent event) {

		caption.setValue(MainLayout.ct().get("contenttool.menu.filter.caption"));
		clear.setCaption(MainLayout.ct().get("contenttool.menu.filter.clear"));
	}
}
